/*
 * @Author: yuki
 * @Date: 2025-10-27 11:46:45
 * @LastEditTime: 2025-10-31 18:12:17
 * @Description: 农历工具
 */
import lunisolar from 'lunisolar';

export type GetBaZiParams = Parameters<typeof lunisolar>;

/**
 * 获取八字
 * @param date 日期
 * @param config 配置
 * @returns
 */
export const getBaZi = (
  date: GetBaZiParams[0],
  config?: GetBaZiParams[1]
): {
  year: string;
  month: string;
  day: string;
  hour: string;
} => {
  const lunarInstance = lunisolar(date, config);
  const year = lunarInstance.char8.year.toString(); // 壬寅  （取得年柱）
  const month = lunarInstance.char8.month.toString(); // 丁未  （取得月柱）
  const day = lunarInstance.char8.day.toString(); // 壬申 （取得日柱）
  const hour = lunarInstance.char8.hour.toString();
  return {
    year: year,
    month: month,
    day: day,
    hour: hour
  };
};

/**
 * 获取八字字符串
 * @param date 日期
 * @param config 配置
 * @returns 八字字符串
 */
export const getBaZiStr = (
  date: GetBaZiParams[0],
  config?: GetBaZiParams[1]
): string => {
  const { year, month, day, hour } = getBaZi(date, config);
  return `${year} ${month} ${day} ${hour}`;
};

export type Lunar2SolarParams = Parameters<typeof lunisolar.fromLunar>;
/**
 * 农历转阳历
 * @param lunarData 农历数据
 * @param config 配置
 * @returns 阳历日期
 */
export const lunar2Solar = (
  lunarData: Lunar2SolarParams[0],
  config?: Lunar2SolarParams[1]
): Date => {
  return lunisolar.fromLunar(lunarData, config).toDate();
};
